package club.calong.sanniu.controller;

import club.calong.sanniu.http.request.InfoRequest;
import club.calong.sanniu.http.response.Response;
import club.calong.sanniu.service.InfoService;
import club.calong.sanniu.util.CheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/info")
public class InfoController {

    InfoService service;

    @Autowired
    public InfoController(InfoService service) {
        this.service = service;
    }

    @GetMapping()
    public Response one(){
        return new Response(Response.SUCCESS, service.one());
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('site:update')")
    public Response update(@RequestBody InfoRequest info){
        if (CheckUtils.checkInfo(info)) {
            if (service.update(info)) {
                return new Response(Response.SUCCESS, null, info);
            }
            return new Response(Response.FAILURE,Response.Message.INSERT_ERROR,null );
        }
        return new Response(Response.FAILURE, Response.Message.VERIFICATION_ERROR,null);
    }
}
